<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","nobkt");
$PRIMARY_KEY    = PRIMARY_KEY;
$PRIMARY_VALUE  = GetParam($PRIMARY_KEY,"");
$FileName       = "ais_approval_cbp.php";
$FileTemplate   = "ais_approval_cbp_form.html";
$SubHeaderTitle = "Transaction Cash/Bank - Payment";
$TableName		= "ais_memorial_z";
$HTMLFormName   = "MemorialTransForm";
$MaxGrid        = "200";
//$HTMLFormName2  = "BudgetForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Grid Fields;
$FormFields    = array("uraian"=>"0",
					   "urut"=>"",
					   "skada"=>"",
					   "jumlah"=>"number",
					   "dk"=>"",
					   "kdact"=>"",
					   "noref"=>"",
					   "memorial_id"=>"");

//buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);
if (GetParam("Trans","")==1) $GOTO = "../transaction/ais_transaction_cashbankpayment.php";
else $GOTO = $FileName;

//************ FORM ACTION **************
if (trim(GetParam("form",""))==trim($HTMLFormName)) {
	if (GetParam("FormAction","")=="Save") {
		list($FDay,$FMonth,$FYear) = explode("/",GetParam("tglbkt",""));
		$tglbkt = "$FYear-$FMonth-$FDay";
		$rate    = StrToNumber (GetParam("rate",""));

		if (trim(GetParam("nobkt",""))=="AUTO NUMBERED BY SYSTEM")	{
			if (GetParam("type","")=="Bank") {
				$ObjNumber  = new autonumber(GetParam("tglbkt",""),GetParam("kdbank",""));
				$TransNo = $ObjNumber->GetNumber("ZBB");				
        FormSave("ais_memorial_z", "jumlah", $FormFields, array("ledger"=>GetPeriod(GetParam("tglbkt","")),"curr"=>GetParam("curr",""),"debtur"=>GetParam("debtur",""),"debnam"=>GetParam("debnam",""),"rate"=>$rate,"tglbkt"=>$tglbkt,"nobkt"=>$TransNo,"ktrasl"=>GetOfficeID(),"status"=>GetParam("status",""),"noorder"=>GetParam("noorder",""),"source"=>GetParam("source",""),"kdbank"=>GetParam("kdbank","")),"memorial_id");
			}else {
				$ObjNumber  = new autonumber(GetParam("tglbkt",""),GetParam("kdcash",""));
				$TransNo = $ObjNumber->GetNumber("ZBK");				
        FormSave("ais_memorial_z", "jumlah", $FormFields, array("ledger"=>GetPeriod(GetParam("tglbkt","")),"curr"=>GetParam("curr",""),"debtur"=>GetParam("debtur",""),"debnam"=>GetParam("debnam",""),"rate"=>$rate,"tglbkt"=>$tglbkt,"nobkt"=>$TransNo,"ktrasl"=>GetOfficeID(),"status"=>GetParam("status",""),"noorder"=>GetParam("noorder",""),"source"=>GetParam("source",""),"kdcash"=>GetParam("kdcash","")),"memorial_id");
      }


			$DBConnection->add_data("ais_memorial_z","source",GetParam("source",""));
			$DBConnection->add_data("ais_memorial_z","urut","999");
			$DBConnection->add_data("ais_memorial_z","ktrasl",GetOfficeID());

			if (GetParam("type","")=="Bank") $DBConnection->add_data("ais_memorial_z","kdbank",GetParam("kdbank",""));
			else $DBConnection->add_data("ais_memorial_z","kdcash",GetParam("kdcash",""));

			$DBConnection->add_data("ais_memorial_z","ledger",GetPeriod(GetParam("tglbkt","")));
			$DBConnection->add_data("ais_memorial_z","curr",GetParam("curr",""));
			$DBConnection->add_data("ais_memorial_z","rate",$rate);			
			$DBConnection->add_data("ais_memorial_z","nobkt",$TransNo);
			$DBConnection->add_data("ais_memorial_z","tglbkt",$tglbkt);
			$DBConnection->add_data("ais_memorial_z","dk",GetParam("dk999",""));
			$DBConnection->add_data("ais_memorial_z","noorder",GetParam("noorder",""));
			$DBConnection->add_data("ais_memorial_z","debtur",GetParam("debtur",""));
			$DBConnection->add_data("ais_memorial_z","debnam",GetParam("debnam",""));
			$DBConnection->add_data("ais_memorial_z","skada",GetParam("skada999",""));
			$DBConnection->add_data("ais_memorial_z","kdact",GetParam("kdact999",""));
			$DBConnection->add_data("ais_memorial_z","uraian",GetParam("uraian999",""));
			$DBConnection->add_data("ais_memorial_z","jumlah",StrToNumber(GetParam("total_trans","")));
			$DBConnection->add_data("ais_memorial_z","rupiah",StrToNumber(GetParam("total_trans","")) * $rate);
			$DBConnection->add_data("ais_memorial_z","noref",GetParam("noref1",""));
			$DBConnection->add_data("ais_memorial_z","status",GetParam("status",""));
			$DBConnection->InsertGrid();

			//$DBConnection->dbc->query("UPDATE ais_memorial_z_history SET memo_jour='".$TransNo."', jml_memo='".StrToNumber(GetParam("total_trans",""))."' WHERE memoesti='".GetParam("aslref","")."' AND ktrasl='".GetOfficeID()."'");


	    } else {
		    $TransNo = GetParam("nobkt","");

		    $DBConnection->dbc->query("DELETE FROM ais_memorial_z WHERE nobkt='".$TransNo."' AND ktrasl='".GetOfficeID()."'");
		    $DBConnection->dbc->query("DELETE FROM ais_hutang_z WHERE nobkt='".$TransNo."' AND ktrasl='".GetOfficeID()."'");

				FormSave("ais_memorial_z", "jumlah", $FormFields, array("ledger"=>GetPeriod(GetParam("tglbkt","")),"curr"=>GetParam("curr",""),"debtur"=>GetParam("debtur",""),"debnam"=>GetParam("debnam",""),"rate"=>$rate,"tglbkt"=>$tglbkt,"nobkt"=>$TransNo,"ktrasl"=>GetOfficeID(),"status"=>GetParam("status",""),"noorder"=>GetParam("noorder",""),"source"=>GetParam("source",""),"kdbank"=>GetParam("kdbank",""),"kdcash"=>GetParam("kdcash","")),"memorial_id");

				$DBConnection->add_data("ais_memorial_z","source",GetParam("source",""));
				$DBConnection->add_data("ais_memorial_z","urut","999");
				$DBConnection->add_data("ais_memorial_z","ktrasl",GetOfficeID());
				$DBConnection->add_data("ais_memorial_z","kdbank",GetParam("kdbank",""));
				$DBConnection->add_data("ais_memorial_z","kdcash",GetParam("kdcash",""));
				$DBConnection->add_data("ais_memorial_z","ledger",GetPeriod(GetParam("tglbkt","")));
				$DBConnection->add_data("ais_memorial_z","curr",GetParam("curr",""));
				$DBConnection->add_data("ais_memorial_z","rate",$rate);			
				$DBConnection->add_data("ais_memorial_z","nobkt",$TransNo);
				$DBConnection->add_data("ais_memorial_z","tglbkt",$tglbkt);
				$DBConnection->add_data("ais_memorial_z","dk",GetParam("dk999",""));
				$DBConnection->add_data("ais_memorial_z","noorder",GetParam("noorder",""));
				$DBConnection->add_data("ais_memorial_z","debtur",GetParam("debtur",""));
				$DBConnection->add_data("ais_memorial_z","debnam",GetParam("debnam",""));
				$DBConnection->add_data("ais_memorial_z","skada",GetParam("skada999",""));
				$DBConnection->add_data("ais_memorial_z","kdact",GetParam("kdact999",""));
				$DBConnection->add_data("ais_memorial_z","uraian",GetParam("uraian999",""));
				$DBConnection->add_data("ais_memorial_z","jumlah",StrToNumber(GetParam("total_trans","")));
				$DBConnection->add_data("ais_memorial_z","rupiah",StrToNumber(GetParam("total_trans","")) * $rate);
				$DBConnection->add_data("ais_memorial_z","noref",GetParam("noref1",""));
				$DBConnection->add_data("ais_memorial_z","status",GetParam("status",""));
				$DBConnection->InsertGrid();

			//$DBConnection->dbc->query("UPDATE ais_memorial_z_history SET memo_jour='".$TransNo."', jml_memo='".StrToNumber(GetParam("total_trans",""))."' WHERE memoesti='".GetParam("aslref","")."' AND ktrasl='".GetOfficeID()."'");

		}
/*
      $SQL = "INSERT INTO ais_hutang_z (ledger,ktrasl,tglbkt,nobkt,urut,noref,nobkta,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,srt) 
                  SELECT ledger,ktrasl,tglbkt,nobkt,urut,noref,noref,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,if(dk='D','2','1') FROM ais_memorial_z WHERE (skada like '3%' OR skada like '4%') AND nobkt='".$TransNo."' ";
      $DBConnection->dbc->query($SQL);
      //$DBConnection->dbc->debug();

      $SQL = "INSERT INTO ais_piutang_z (ledger,ktrasl,tglbkt,nobkt,normut,noref,nobkta,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,srt) 
                  SELECT ledger,ktrasl,tglbkt,nobkt,urut,noref,noref,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,if(dk='D','1','2') FROM ais_memorial_z WHERE (skada like '0006%' OR skada like '0007%') AND nobkt='".$TransNo."' ";
      $DBConnection->dbc->query($SQL);
*/
		//DeleteRecord ("txtCheck",$MaxGrid);
		if (GetParam("Trans","")) header("Location: ../transaction/ais_transaction_cashbankpayment.php");
		else header("Location: $FileName?PageMode=Form&".PRIMARY_KEY."=".$TransNo."&ttype=".GetParam("type",""));
		exit;

	}

	if (GetParam("FormAction","")=="Delete") {
//		print "Deleted"."<br>\n";
		//DeleteRecord ("txtCheck",$MaxGrid);
                DeleteTrans(GetParam("nobkt",""));
                header("Location: $FileName?");               
                exit;
                
	}

	if (GetParam("FormAction","")=="Approval") {
			$TransNo = GetParam("nobkt","");
			SetTransApproval(GetParam("Type",""),$TransNo);
			header("Location: $FileName");
			exit;
	}
}

//Field Verification 
$Verification  = "False";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields = array(
					  "source"=>"Source",
					  "nobkt"=>"ID No.",
					  "tglbkt"=>"Transaction Date",
					  "curr"=>"Currency",
					  "uraian1"=>"Uraian On Line 1",
					  "skada1"=>"Skada On Line 1",
					  "uraian999"=>"Uraian On Line 999",
					  "noref999"=>"Ref On Line 999"
					  );
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar,array("debtur"=>"Debtur"));

if (GetParam(PRIMARY_KEY,"")) {
	$SQLCount     = "SELECT count(*) FROM `$TableName`";
	$SQLCount    .= "WHERE ".PRIMARY_KEY."='".$PRIMARY_VALUE."' AND urut<>999";
	$Count        = $DBConnection->dbc->get_var($SQLCount);
} else 
	$Count=0;

$NewDataStart = $Count + 1;
//$NewDataStart = 1;
$InitFunction = "Masking();Masking2();";

$VerifyGrids = array("uraian"=>"0","jumlah"=>"number","skada"=>"0");
$Masking1 = BuildMasking ($HTMLFormName,MutatorFields($FormFields,($MaxGrid+$Count)));
$Masking2 = BuildMasking ($HTMLFormName,array("tglbkt"=>"date","rate"=>"number"),"2");
$JSMasks  = $Masking1.$Masking2;

Init($SessionVar,$JSMasks);
FormGridHTML ($HTMLFormName,$VerifyGrids,$Count,$MaxGrid,$SessionVar);

//************ SET FORM MODE ***************
if (GetParam(PRIMARY_KEY,"")) {
	$FormMode = "Edit";
	if (CheckisApproved("Approval1",GetParam(PRIMARY_KEY,""))) $USER_CANAPPROVAL1 = "";
	if (CheckisApproved("Approval2",GetParam(PRIMARY_KEY,""))) $USER_CANAPPROVAL2 = "";
	if (CheckisApproved("Approval3",GetParam(PRIMARY_KEY,""))) $USER_CANAPPROVAL3 = "";
} else {
	$FormMode = "Add";
	$USER_CANAPPROVAL1 = "";
	$USER_CANAPPROVAL2 = "";
	$USER_CANAPPROVAL3 = "";
}

//******* DATABASE GRID QUERY ***********

$SQL = "SELECT ".BuildFields($FormFields)." FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQL      = $SQL."WHERE ".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."'";
    $SQL     .= " AND ktrasl='".GetOfficeID()."'";
    $SQLGrid  = $SQL." ORDER BY ".PRIMARY_KEY;
}


//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

//********************************* HTML Header ******************************************
//Select Currency Table
$SQLCurr = "SELECT * FROM tblcurrency";
//Select Department:
$SQLDept = "SELECT * FROM tbldept";
//Select Receipt:
$SQLReceipt = "SELECT * FROM tblreceipt";
//Select Bank:
$SQLBank = "SELECT * FROM tblbank ORDER BY banknam";
//Select Cash:
$SQLCash = "SELECT * FROM tblcash ORDER BY cashnam";
//Select Activity
$SQLAct  = "SELECT  * FROM tblactivity ORDER BY actname";

$HeaderFields = array(
											"source"=>"",
										  "nobkt"=>"0",
										  "tglbkt"=>"date",
										  "curr"=>"",
										  "rate"=>"number",
										  "debtur"=>"",
										  "debnam"=>"",
										  "kdcash"=>"",
										  "kdbank"=>"",
										  "noorder"=>""
										  );
//Leave Here Untouch
$FormHName     = BuildHTMLField($HeaderFields,"name",1);
$FormHValue    = BuildHTMLField($HeaderFields,"value",1);
$SQLHeader     = "SELECT ".BuildFields($HeaderFields)." FROM $TableName ";

if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") $SQLHeader .= "WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."' AND ktrasl='".GetOfficeID()."'";
$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeBlock("bankblk",$DBMysql,$SQLBank);
if (GetParam("ttype","")) $ttype = GetParam("ttype","");
elseif (GetParam("nobkt","")) {
	$SQL = "SELECT IF(kdcash='','Bank','Cash') FROM ais_memorial_z WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."' AND urut=999 ";
	$ttype = $DBConnection->dbc->get_var($SQL);
} else $ttype = "Bank";

$TBS->MergeBlock("cashblk",$DBMysql,$SQLCash);
$TBS->MergeBlock("currformblk",$DBMysql,$SQLCurr);
$TBS->MergeBlock("receiptblk",$DBMysql,$SQLReceipt);
$TBS->MergeBlock("actblk",$DBMysql,$SQLAct);
$TBS->MergeBlock("actblk9",$DBMysql,$SQLAct);

//Total 999 Values
/*
$Fields9       = array("noref999"=>"0","jumlah999"=>"number","skada999"=>"0","kdact999"=>"0");
$Form9Value    = BuildHTMLField($Fields9,"value",1);
*/

$SQL999 = "SELECT * FROM ais_memorial_z "."WHERE ".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."' AND ktrasl='".GetOfficeID()."' ";
$SQL999 = $SQL999." AND urut = 999";

$TBS->MergeBlock("dept999blk",$DBMysql,$SQLDept);
if (GetParam(PRIMARY_KEY,"")) {$TBS->MergeBlock("blg9",$DBMysql,$SQL999);}
else $TBS->MergeBlock("blg9",$FormCValue);

// HTML Value
$TBS->MergeBlock("headbln",$FormHName);
if (GetParam(PRIMARY_KEY,"")) {$TBS->MergeBlock("headblv",$DBMysql,$SQLHeader);}
else $TBS->MergeBlock("headblv",$FormHValue);

//********************************* HTML GRID *********************************************
//HTML Grid
$SQLGrid = "SELECT * FROM ais_memorial_z "."WHERE ".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."' AND ktrasl='".GetOfficeID()."' AND urut<>999";
//print $SQLGrid;

$Result  = $DBConnection->dbc->get_results($SQLGrid,ARRAY_A);
$Result  = BuildEmptyGrid ($FormFields, $MaxGrid, $Result);

//$Result  = BuildEmptyGrid ($FormFields, $MaxGrid, $Result);

$TBS->MergeBlock("blg",$Result);
$TBS->Show();

//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->deleteGrid("$TableName","nobkt");
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}

function DeleteTrans($nobkt) {
global $DBConnection;
                       
                        $SQL = "DELETE FROM ais_memorial_z WHERE nobkt='$nobkt' ";
                        $DBConnection->dbc->query($SQL);
                        
                        $SQL = "DELETE FROM ais_hutang_z WHERE nobkt='$nobkt' ";
                        $DBConnection->dbc->query($SQL);

}

function FormSaveHutang ($table, $keyField, $FormFields, $SpecialCase="", $PrimaryKey) {
	global $FileName, $MaxGrid, $DBConnection;
	$TotalData = $MaxGrid * 2;	
	$NewDataStart = GetParam("NewData","0");
//	$NewDataStart = 1;
	for ($a=1;$a<=$TotalData;$a++) {
		foreach ($FormFields as $key=>$formatting) {
			if (!GetParam($keyField.$a,"")) {
				$EndOfData = 1;
				break;
			}
			$value = GetParam($key.$a,"");
			if ($formatting=="number") $value = str_replace(",","",$value);
			if ($formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
		    }
			$DBConnection->add_data($table,$key,$value);
		}
		$rate = StrToNumber(GetParam("rate",""));
		$jmh  = StrToNumber(GetParam("jumlah".$a,""));
		$rupiah = $rate * $jmh;
		$DBConnection->add_data($table,"rupiah",$rupiah);
		$DBConnection->add_data($table,"rupiah",$rupiah);
		if ($EndOfData) {
			break;
	    }
		if (strlen($SpecialCase)) {
			foreach ($SpecialCase as $key => $value) {
			//	print "add_data($table,$key,$value);<br>";
				$DBConnection->add_data($table,$key,$value);
			}
		}
//		print "$a < $NewDataStart<br>";
		if ($a < $NewDataStart) {
			 if (strlen($PrimaryKey)) {
			    $DBConnection->UpdateGrid($PrimaryKey,GetParam($PrimaryKey.$a,""));
		 	 } else {
			    $DBConnection->UpdateGrid($keyField,GetParam($keyField.$a,""));
		     }
		} else {
			$DBConnection->InsertGrid();
	    }
	}
	$DBConnection->clearData();
}


function FormSave ($table, $keyField, $FormFields, $SpecialCase="", $PrimaryKey) {
	global $FileName, $MaxGrid, $DBConnection;
	$TotalData = $MaxGrid * 2;	
	$NewDataStart = GetParam("NewData","0");
	$NewDataStart = 1;
	for ($a=1;$a<=$TotalData;$a++) {
		foreach ($FormFields as $key=>$formatting) {
			if (!GetParam($keyField.$a,"")) {
				$EndOfData = 1;
				break;
			}
			$value = GetParam($key.$a,"");
			if ($formatting=="number") $value = str_replace(",","",$value);
			if ($formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
		    }
			$DBConnection->add_data($table,$key,$value);
		}
		$rate = StrToNumber(GetParam("rate",""));
		$jmh  = StrToNumber(GetParam("jumlah".$a,""));
		$rupiah = $rate * $jmh;
		$DBConnection->add_data($table,"rupiah",$rupiah);
		if ($EndOfData) {
			break;
	    }
		if (strlen($SpecialCase)) {
			foreach ($SpecialCase as $key => $value) {
			//	print "add_data($table,$key,$value);<br>";
				$DBConnection->add_data($table,$key,$value);
			}
		}
//		print "$a < $NewDataStart<br>";
		if ($a < $NewDataStart) {
			 if (strlen($PrimaryKey)) {
			    $DBConnection->UpdateGrid($PrimaryKey,GetParam($PrimaryKey.$a,""));
		 	 } else {
			    $DBConnection->UpdateGrid($keyField,GetParam($keyField.$a,""));
		     }
		} else {
			$DBConnection->InsertGrid();
			//$DBConnection->dbc->debug();
	    }
	}
	$DBConnection->clearData();
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		for ($x=0;$x<count($arrayfield);$x++) {
			$DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
		}
}

function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->deleteGrid("$TableName","receipt_id",$sequence_name.$x);
	    }
	}
}

function BuildFields ($FormFields) {
		global $DBConnection;
		$str = "";
		foreach ($FormFields as $key=>$formatting) {
			if (strlen($str)) $str .= ",$key";
			else $str = "$key";
		}
		return $str;
}

function MutatorFields ($FormFields, $MaxGrid) {
		global $DBConnection;
		$array_fields = array();
		for ($x=0;$x<$MaxGrid;$x++) {
			foreach ($FormFields as $key=>$formatting){
				$a=$x+1;
				$array_fields[$key.$a] = "$formatting";
			}			
		}
		return $array_fields;
}

//APROVAL FUNCTIION BEGIN
function SetTransApproval($appr_level,$nobkt) {
	global $DBConnection;
	if (!(ereg("TB",$nobkt) || ereg("TK",$nobkt) || ereg("BB",$nobkt) || ereg("BK",$nobkt))) return;
	if ($appr_level=="Approval1") {
		CheckAndUpdateAppr("Approval1",$nobkt);
		SetLogActivity($nobkt,"Approval1","");
	}elseif ($appr_level=="Approval2") {
		CheckAndUpdateAppr("Approval1",$nobkt);
		CheckAndUpdateAppr("Approval2",$nobkt);
		MoveRecord($nobkt);
		SetLogActivity($nobkt,"Approval2","");
	}else {
		CheckAndUpdateAppr("Approval1",$nobkt);
		CheckAndUpdateAppr("Approval2",$nobkt);
		CheckAndUpdateAppr("Approval3",$nobkt);
	}
}

function GetTransDate($nobkt) {
  global $DBConnection;
  $SQL = "SELECT date_format(tglbkt,'%d/%m/%Y') FROM ais_memorial_z WHERE nobkt='$nobkt' ";
  return $DBConnection->dbc->get_var($SQL);
}


function MoveRecord($nobkt) {
		global $DBConnection;
		//Set Number on tblnumbering		
		ereg("TB|TK|BB|BK",$nobkt,$Mod);
		if (($Mod[0]=="TB")||($Mod[0]=="BB")) {
			$GetCode = $DBConnection->dbc->get_var("SELECT kdbank FROM ais_memorial_z WHERE nobkt='$nobkt'");
		}elseif (($Mod[0]=="TK")||($Mod[0]=="BK")) {
			$GetCode = $DBConnection->dbc->get_var("SELECT kdcash FROM ais_memorial_z WHERE nobkt='$nobkt'");
		}

    $JumlahToBalance = $DBConnection->dbc->get_var("SELECT jumlah FROM ais_memorial_z WHERE nobkt='$nobkt' AND urut=999");
		$CurrentDate = GetTransDate($nobkt);
		if (strlen($GetCode))
		$ObjNumber  = new autonumber($CurrentDate,$GetCode);
		else 
		$ObjNumber  = new autonumber($CurrentDate);
		$TransNo = $ObjNumber->GetNumber($Mod[0]);			

		$Ledger = GetPeriod($CurrentDate);

		
		//Update Number on ais_memorial_z
		$SQL = "UPDATE ais_memorial_z SET nobkt='$TransNo' WHERE nobkt='$nobkt'";
		$DBConnection->dbc->query($SQL);
                //Update Number on ais_piutang_z
		$SQL = "UPDATE ais_hutang_z SET nobkt='$TransNo' WHERE nobkt='$nobkt'";
		$DBConnection->dbc->query($SQL);
                
		//Migrate data from ais_memorial_z to ais_memorial
		$SQL = "INSERT INTO ais_memorial (".GetColumnStruct("ais_memorial", "memorial_id").") SELECT ".GetColumnStruct("ais_memorial", "memorial_id")." FROM ais_memorial_z WHERE nobkt='$TransNo'";
		$DBConnection->dbc->query($SQL);		

/*                
		$SQL = "INSERT INTO ais_hutang SELECT * FROM ais_hutang_z WHERE nobkt='$TransNo'";
		$DBConnection->dbc->query($SQL);		
*/
/*
    $SQL = "INSERT INTO ais_hutang (ledger,ktrasl,tglbkt,nobkt,urut,noref,nobkta,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,srt) 
                SELECT ledger,ktrasl,tglbkt,nobkt,urut,noref,noref,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,if(dk='D','2','1') FROM ais_memorial_z WHERE (skada like '3%' OR skada like '4%') AND nobkt='".$TransNo."' ";
    $DBConnection->dbc->query($SQL);
    //$DBConnection->dbc->debug();

    $SQL = "INSERT INTO ais_piutang (ledger,ktrasl,tglbkt,nobkt,normut,noref,nobkta,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,srt) 
                SELECT ledger,ktrasl,tglbkt,nobkt,urut,noref,noref,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,if(dk='D','1','2') FROM ais_memorial_z WHERE (skada like '0006%' OR skada like '0007%') AND nobkt='".$TransNo."' ";
    $DBConnection->dbc->query($SQL);
*/
    SaveAging($TransNo);

		//Delete ais_memorial_z
		$SQL = "DELETE FROM ais_memorial_z WHERE nobkt='$TransNo'";
		$DBConnection->dbc->query($SQL);
/*                
		$SQL = "DELETE FROM ais_hutang_z WHERE nobkt='$TransNo'";
		$DBConnection->dbc->query($SQL);
*/                
                //Update Bank Or Cash
                if ($Mod[0]=="BB") {
                  $SQL = "UPDATE tblbank SET balance=balance-$JumlahToBalance WHERE kdbank='".$GetCode."'" ;
                } elseif ($Mod[0]=="BK") {
                  $SQL = "UPDATE tblcash SET balance=balance-$JumlahToBalance WHERE kdcash='".$GetCode."'" ;
                }
                $DBConnection->dbc->query($SQL);

                //Update History Transaction
                $SQL = "SELECT * FROM ais_memorial WHERE nobkt='$TransNo' AND urut=999";
                $MyResult = $DBConnection->dbc->get_row($SQL);

                if ($MyResult) {
                    if (ereg("BS",$MyResult->noref)) {
                        $SQL = "UPDATE ais_advance SET aslref='".$TransNo."' WHERE nobkt='".$MyResult->noref."'";
                        $DBConnection->dbc->query($SQL);
                    }
                /*
                  $SQL = "UPDATE ais_memorial_history SET memo_jour='".$MyResult->nobkt."', jml_memo='".$MyResult->jumlah."' WHERE memoesti='".$MyResult->aslref."' AND ktrasl='".GetOfficeID()."'";
                  $DBConnection->dbc->query($SQL);
                  $DBConnection->dbc->debug();
                  */
                }

	
}

function CheckisApproved($appr_level,$nobkt){
	global $DBConnection;
	if ($appr_level=="Approval1") $f_app = "app1";
	elseif ($appr_level=="Approval2") $f_app = "app2";
	elseif ($appr_level=="Approval3") $f_app = "app3";
	
	$SQL = "SELECT $f_app FROM ais_memorial_z WHERE nobkt='$nobkt'";
	if ($DBConnection->dbc->get_var($SQL)==1) return true;
	else return false;
}



function CheckAndUpdateAppr($appr_level,$nobkt){
	global $DBConnection,$FileName,$TableName;	
	if ($appr_level=="Approval1") {
		$SQL = "SELECT app1 FROM ais_memorial_z WHERE nobkt='$nobkt'";
		$ApprStat = $DBConnection->dbc->get_var($SQL);
		if ($ApprStat<>'1') 
			$SQL = "UPDATE ais_memorial_z SET app1='1', nmapp1='".GetStaffName()."' WHERE nobkt = '$nobkt'";
		else return false;

	} elseif ($appr_level=="Approval2") {
		$SQL = "SELECT app2 FROM ais_memorial_z WHERE nobkt='$nobkt'";
		$ApprStat = $DBConnection->dbc->get_var($SQL);
		if ($ApprStat<>'1') 
			$SQL = "UPDATE ais_memorial_z SET app2='1', nmapp2='".GetStaffName()."' WHERE nobkt = '$nobkt'";
		else return false;
	} else {
		$SQL = "SELECT app3 FROM ais_memorial_z WHERE nobkt='$nobkt'";
		$ApprStat = $DBConnection->dbc->get_var($SQL);
		if ($ApprStat<>'1') 
			$SQL = "UPDATE ais_memorial_z SET app3='1', nmapp3='".GetStaffName()."' WHERE nobkt = '$nobkt'";
		else return false;
	}
	$DBConnection->dbc->query($SQL);
}

?>
